package com.dxf.securitys.config;

import com.dxf.common.exception.BizException;
import com.dxf.common.exception.CommonEnum;
import com.dxf.common.utils.GlobalConstant;
import com.dxf.common.utils.RedisUtil;
import com.dxf.securitys.entity.SecurityUser;
import com.dxf.securitys.utils.SecurityUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.Arrays;
import java.util.List;

@Slf4j
@Service(value = "el")
public class ElPermissionConfig {

    @Autowired
    RedisUtil redisUtil;

    public Boolean check(String ...permissions) {
        String name=SecurityUtils.getCurrentUser();
        log.info("需要权限：{}",permissions);

        // 获取当前用户的所有权限
        SecurityUser user= (SecurityUser) redisUtil.get(name);
        List<String> elPermissions = user.getPermissionValueList();

        Boolean flag= name.equals(GlobalConstant.EMAIL_SETFROM)
                || Arrays.stream(permissions).anyMatch(elPermissions::contains);
        if (!flag){
            throw  new BizException(CommonEnum.NO_PERMISSION);
        }
        // 判断当前用户的所有权限是否包含接口上定义的权限
        return  flag;
    }

}
